/*
 * NativeTestRegistry.cpp  0.4.0 / Nov 5, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "NativeTestRegistry.h"

#ifdef CUTE4J_TEST

#include "QStringListAdapter.h"

// NativeTestRegistry :: Private Constructor. ----------------------------------

NativeTestRegistry::NativeTestRegistry()
{
  // disabled constructor
}

// NativeTestRegistry :: Private Destructor. -----------------------------------

NativeTestRegistry::~NativeTestRegistry()
{
  // disabled destructor
}

// NativeTestRegistry :: Public Static Member Functions. -----------------------

/*!
 * \brief Registers native test factory.
 *
 * \param module the module name
 * \param testFactory the test factory
 */
bool NativeTestRegistry::
                     registerTestFactory(const QString &module,
                                         AbstractNativeTestFactory *testFactory)
{
  if (!modules) {
    modules = new NativeTestModules;
    NativeTestFactories *factories = new NativeTestFactories;
    modules->insert(module, factories);
    factories->append(testFactory);
  } else if (testFactory) {
    NativeTestFactories *factories = modules->value(module);
    if (!factories) {
      factories = new NativeTestFactories;
      modules->insert(module, factories);
    } else if (factories->contains(testFactory)) {
      return false;
    }
    factories->append(testFactory);
  }
  return true;
}

/*!
 * \brief Returns native test modules.
 *
 * \param env the JNIEnv pointer
 *
 * \return native test modules
 */
jobjectArray NativeTestRegistry::getTestModules(JNIEnv *env)
{
  if (!modules) {
    modules = new NativeTestModules;
  }
  return QStringListAdapter::toJStringArray(env, modules->keys());
}

/*!
 * \brief Returns test cases of the specified test module.
 *
 * \param env the JNIEnv pointer
 * \param testModule the test module
 *
 * \return test cases of the specified test module
 */
jobjectArray NativeTestRegistry::getTestCases(JNIEnv *env, jstring testModule)
{
  if (!modules) {
    modules = new NativeTestModules;
  }
  QStringList testCases;
  NativeTestFactories *factories = modules->value(QStringAdapter(env,
                                                                 testModule));
  if (factories) {
    for (int i = 0; i < factories->size(); i++) {
      testCases.append(factories->at(i)->name());
    }
  }
  return QStringListAdapter::toJStringArray(env, testCases);
}

/*!
 * \brief Creates test case.
 *
 * \param env the JNIEnv pointer
 * \param testModule the test module
 * \param testCase the test case
 *
 * \return test case
 */
jlong NativeTestRegistry::createTestCase(JNIEnv *env, jstring testModule,
                                         jstring testCase)
{
  if (!modules) {
    modules = new NativeTestModules;
  }
  NativeTestFactories *factories = modules->value(QStringAdapter(env,
                                                                 testModule));
  if (factories) {
    QStringAdapter testCaseName(env, testCase);
    for (int i = 0; i < factories->size(); i++) { // TODO: implement hash search
      AbstractNativeTestFactory *testFactory = factories->at(i);
      if (testFactory->name() == testCaseName) {
        return reinterpret_cast<jlong>(testFactory->createNativeTest());
      }
    }
  }
  return 0;
}

// NativeTestRegistry :: Private Static Data Member. ---------------------------

NativeTestModules *NativeTestRegistry::modules = 0; 

#endif // CUTE4J_TEST
